package com.cml.dm.engine.config;

import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.cml.dm.engine.context.DmContext;
import com.cml.dm.engine.model.Task;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

@Slf4j
public class DmEngineXmlParser {
    public static void parser(List<Task> taskList, String filename){
        InputStream is = null;
        Document document = null;
        try {
            SAXReader saxReader = new SAXReader();
            is = DmEngineXmlParser.class.getClassLoader().getResourceAsStream(filename);
            document = saxReader.read(is);
            Element root = document.getRootElement();

            for(Iterator i = root.elementIterator(); i.hasNext();){
                Element elTemp = (Element) i.next();
                XmlNodeParserFactory.getXmlNodeParser(elTemp).parser(taskList,elTemp);
            }

        }catch (Exception e) {
            log.error("读取"+filename+"文件报错，请检查文件是否存在！",e);
        }finally {
            IoUtil.close(is);
        }
    }
}
